package com.jinyu.jyly.ui.mine.setting.changemobile;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 修改手机号
 */
public class ChangeMobile01Activity extends BaseActivity<ChangeMobilePresenter> implements ChangeMobileContract.View {


    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.tv_mobile)
    EditText tvMobile;
    @BindView(R.id.btn_sub)
    Button btnSub;
    private ChangeMobilePresenter presenter;
    private int mSmsId;

    @Override
    public int getLayout() {
        return R.layout.activity_change_mobile_01;
    }

    @Override
    protected ChangeMobilePresenter getPresenter() {
        if (presenter == null) {
            presenter = new ChangeMobilePresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
    }


    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    @Override
    public void onChangeMobile(AllInfoBean infoBean) {
        mTipDialog.dismiss();
    }

    @Override
    public void onSiteMakeSms(AllInfoBean infoBean) {
        mTipDialog.dismiss();
    }

    /**
     * 修改联系人手机号
     * @param infoBean
     */
    @Override
    public void onChangeContractMobile(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()){
            case 200:
                String data = infoBean.getData();
                showToast("修改成功");
                finish();
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }


    @OnClick({R.id.iv_back, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_sub:
                String mobile = tvMobile.getText().toString().trim();

                if(TextUtils.isEmpty(mobile)){
                    showToast("手机号不能为空");
                    return;
                }
                boolean showing = mTipDialog.isShowing();
                if(!showing){
                    try {
                        mTipDialog.show();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("mobile", mobile);
                        String pamam = AES.encryptToHexString(jsonObject.toString());
                        presenter.changeContractMobile(pamam);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
